package neusoft.example.com.yuejian06.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import neusoft.example.com.yuejian06.Activity.CompetitionDetailActivity;
import neusoft.example.com.yuejian06.Activity.PictureDetailActivity;
import neusoft.example.com.yuejian06.Bean.Competition;
import neusoft.example.com.yuejian06.PublicActivity;
import neusoft.example.com.yuejian06.R;

public class CompetitionListAdapter extends RecyclerView.Adapter {
    private PublicActivity publicActivity;
    private List<Competition> competitionList;
    private Context context;

    public void setData(List<Competition> list){
        competitionList=list;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater=LayoutInflater.from(parent.getContext());
        View itemView=inflater.inflate(R.layout.competition_item,parent,false);
        CompetitionListAdapter.ItemViewHolder viewHolder=new CompetitionListAdapter.ItemViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        publicActivity=new PublicActivity();
        final CompetitionListAdapter.ItemViewHolder viewHolder=(CompetitionListAdapter.ItemViewHolder)holder;
        final Competition competition=competitionList.get(position);

        String competition_name=competition.getCompetition_name();
        String competition_starttime=competition.getCompetition_starttime();
        String competition_endtime=competition.getCompetition_endtime();
        String competition_pic=competition.getCompetition_pic();

        viewHolder.competition_name.setText(competition_name);
        viewHolder.competition_starttime.setText(competition_starttime);
        Picasso.with(viewHolder.imageView.getContext())
                .load(publicActivity.getIp()+"static/picture/"+ competition.getCompetition_pic())
                .into(viewHolder.imageView);

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                int position=viewHolder.getLayoutPosition();
                Intent intent=new Intent(v.getContext(), CompetitionDetailActivity.class);
                intent.putExtra("competition_id",competition.getCompetition_id());
                v.getContext().startActivity(intent);

            }
        });

    }

    @Override
    public int getItemCount() {
        if(competitionList!=null)
            return competitionList.size();
        else
            return 0;
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        public ImageView imageView;
        public TextView competition_name,competition_starttime,competition_endtime;
        public View itemView;
        public ItemViewHolder(View itemView) {
            super(itemView);
            this.itemView=itemView;
            imageView=(ImageView)itemView.findViewById(R.id.pic);
            competition_name=(TextView)itemView.findViewById(R.id.name);
            competition_starttime=(TextView)itemView.findViewById(R.id.time);
            //competition_endtime=(TextView)itemView.findViewById(R.id.address);
        }
    }
}
